import { Inject, Injectable } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { InjectConnection } from '@nestjs/sequelize';
import { QueryTypes } from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
@Injectable()
export class EssayService {
  constructor(
    @InjectConnection('resume')
    private sequelize: Sequelize,

    @Inject(REQUEST)
    private readonly request: Request & { user: { userId: number } },
  ) {}

  async getRecommend(params) {
    const { pageNum, pageSize } = params;
    const sql = `select id, title, description, img  from  resume_eassay_list where flag=1  limit ${
      +pageSize * (+pageNum - 1)
    } , ${pageSize};`;

    const sql2 = ` SELECT COUNT(id) AS TOTAL FROM resume_eassay_list where flag=1;`;
    const total = await this.sequelize.query(sql2, {
      type: QueryTypes.SELECT,
    });
    const result = await this.sequelize.query(sql, {
      type: QueryTypes.SELECT,
    });
    return {
      total: total[0]?.['TOTAL'],
      data: result,
    };
  }
  async getRecommendDetail(params) {
    const { id } = params;
    const sql = `select  id, title, description, content from  resume_eassay_detail where pid = ${id};`;

    const res = await this.sequelize.query(sql, {
      type: QueryTypes.SELECT,
    });
    return res?.[0];
  }
  async getFanwen(params) {
    const { pageNum, pageSize } = params;
    const sql = `select id, title, description, img  from  resume_model_essay_list order by id desc limit ${
      +pageSize * (+pageNum - 1)
    } , ${pageSize};`;

    const sql2 = ` SELECT COUNT(id) AS TOTAL FROM resume_model_essay_list`;
    const total = await this.sequelize.query(sql2, {
      type: QueryTypes.SELECT,
    });
    const result = await this.sequelize.query(sql, {
      type: QueryTypes.SELECT,
    });
    return {
      total: total[0]?.['TOTAL'],
      data: result,
    };
  }
  async getFanwenDetail(params) {
    const { id } = params;
    const sql = `select  id, title, description, content from  resume_model_essay_detail where pid = ${id};`;

    const res = await this.sequelize.query(sql, {
      type: QueryTypes.SELECT,
    });
    return res?.[0];
  }
}
